var orderStatusa = null;
var table = null;
//返回主页面
$("#fh").click(function () {
    window.location = '';
})
//页面刷新
$("#sx").click(function () {
    window.location = 'Order.html';
})

function refresh() {
    tableIn.reload('btable')
}
var tableIn = null;
layui.use(['table','form'],function() {
    table = layui.table;
    var form = layui.form;

    tableIn = table.render({
        elem: '#demo',
        url: '/order/getOrder',//数据接口
        id: 'btable',
        page: {
            layout: ['count', 'prev', 'page', 'next', 'limit'],//展示按钮 以及按钮的顺序
            groups: 5,
            limit: 5,// 每条的页数
            limits: [5, 10, 20] // 可选的每条的页数
        },
        cols: [
            [
                {field: '', title: '', type: 'checkbox'},
                {field: 'orderId', title: '序号', height: '120'},
                {field: 'orderReference', title: '订单编号',  height: '120'},
                {field: 'userNickname', title: '用户昵称', height: '120'},
                {field: 'commodityName', title: '商品名称', height: '120'},
                {field: 'orderTotal', title: '订单金额', height: '120'},
                {field: 'orderPayment', title: '支付方式', height: '120',templet: function(row){
                        if(row.orderPayment == '1'){
                            return "微信支付";
                        }if(row.orderPayment == '2'){
                            return"余额支付";
                        }if(row.orderPayment == '3'){
                            return"支付宝支付";
                        }if(row.orderPayment == '4'){
                            return"未支付";
                        }
                    }
                },
                {field: 'orderSource', title: '订单来源',  height: '120',templet: function(row){
                        if(row.orderSource == '2'){
                            return "小程序";
                        }if(row.orderSource == '3'){
                            return"APP";
                        }
                    }},
                {field: 'orderStatus', title: '订单状态',  height: '120',templet: function(row) {
                        if (row.orderStatus == '2') {
                            return "待付款";
                        }if (row.orderStatus == '3') {
                            return "已付款";
                        }if (row.orderStatus == '4') {
                            return "待发货";
                        }if (row.orderStatus == '5') {
                            return "已发货";
                        }if (row.orderStatus == '6') {
                            return "已签收";
                        }if (row.orderStatus == '7') {
                            return "退货申请";
                        }if (row.orderStatus == '8') {
                            return "退货中";
                        }if (row.orderStatus == '9') {
                            return "已退货";
                        }if (row.orderStatus == '10') {
                            return "取消交易";
                        }if (row.orderStatus == '11') {
                            return "拒绝退货";
                        }if (row.orderStatus == '12') {
                            return "待评论";
                        }if (row.orderStatus == '13') {
                            return "已完成";
                        }

                    }
                },
                {field: 'orderSubmissionTime', title: '提交时间', height: '120'},
                {toolbar: '#tooldemo', title: '操作', height: '120'}
            ]
        ],
        parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.count,
                "data": res.data
            }
        },
        toolbar: '#toolbarbtn'
        ,where:{
            orderStatus:4
        }
    })
    //模糊查询（和分页查询写在一起的）
    form.on('submit(go)',function (d){
        d = d.field;
        console.log(d);
        table.reload('btable',{
            where:{
                orderStatus:orderStatusa,
                orderReference:d.orderReference,
                userNickname:d.userNickname,
                commodityName:d.commodityName
            }
        });
    })


    //单击查看按钮
    table.on('tool(userdemo)', function (obj) {
        var e = obj.event,
            data = obj.data;
        console.log(e)
        console.log(data)
        if (obj.event === 'ck') {
            //layer.msg("查看")
            layer.open({
                type: 2,
                title: '查看',
                content: 'examinee.html',
                area: ['75%', '85%'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    body.find('#orderReference').val(obj.data.orderReference);
                    body.find('#orderId').val(obj.data.orderId);
                    // body.find('#').attr('src', '../../../img/' + obj.data.);
                    body.find('#commodityName').val(obj.data.commodityName);
                    body.find('#commodityNamee').val(obj.data.commodityName);
                    body.find('#userAccount').val(obj.data.userAccount);
                    body.find('#orderPayment').val(obj.data.orderPayment);
                    body.find('#orderType').val(obj.data.orderType);
                    body.find('#orderDistribution').val(obj.data.orderDistribution);
                    body.find('#orderNumber').val(obj.data.orderNumber);
                    body.find('#orderConsignee').val(obj.data.orderConsignee);
                    body.find('#orderCellphone').val(obj.data.orderCellphone);
                    body.find('#orderSite').val(obj.data.orderSite+""+obj.data.orderCity+""+obj.data.orderDistrict+""+obj.data.orderDetail);
                    body.find("div[name = commodityImage]").html('<img src="'+obj.data.commodityImage+'", ' +
                        'style="width: 100px;height: 100px">');
                    // body.find('#commodityImage').val(obj.data.commodityImage);
                    body.find('#commodityCode').val(obj.data.commodityCode);
                    body.find('#currentPrice').val(obj.data.currentPrice);
                    body.find('#commodityQuantity').val(obj.data.commodityQuantity);
                    body.find('#commodityLabel').val(obj.data.commodityLabel);
                    body.find('#commoditySubtotal').val(obj.data.commoditySubtotal);
                    body.find('#orderFreight').val(obj.data.orderFreight);
                    body.find('#orderDiscounts').val(obj.data.orderDiscounts);
                    body.find('#orderTotal').val(obj.data.orderTotal);
                    body.find('#orderPocket').val(obj.data.orderPocket);
                    if(obj.data.orderStatus == 2){
                        body.find('#statusl').html("当前订单状态：待付款");
                        body.find('#orderStatus').val("待付款");
                    }else if(obj.data.orderStatus == 3){
                        body.find('#statusl').html("当前订单状态：已付款");
                        body.find("#Buttonll").removeClass("dipy");
                        body.find('#orderStatus').val("已付款");
                    }else if(obj.data.orderStatus == 4){
                        body.find('#statusl').html("当前订单状态：待发货");
                        body.find("#Buttonll").removeClass("dipy");
                        body.find('#orderStatus').val("待发货");
                    }else if(obj.data.orderStatus == 5){
                        body.find('#statusl').html("当前订单状态：已发货");
                        body.find('#orderStatus').val("已发货");
                    }else if(obj.data.orderStatus == 6){
                        body.find('#statusl').html("当前订单状态：已签收");
                        body.find('#orderStatus').val("已签收");
                    }else if(obj.data.orderStatus == 7){
                        body.find('#statusl').html("当前订单状态：退货申请");
                        body.find('#orderStatus').val("退货申请");
                    }else if(obj.data.orderStatus == 8){
                        body.find('#statusl').html("当前订单状态：退货中");
                        body.find('#orderStatus').val("退货中");
                    }else if(obj.data.orderStatus == 9){
                        body.find('#statusl').html("当前订单状态：已退货");
                        body.find('#orderStatus').val("已退货");
                    }else if(obj.data.orderStatus == 10){
                        body.find('#statusl').html("当前订单状态：取消交易");
                        body.find('#orderStatus').val("取消交易");
                    }else if(obj.data.orderStatus == 11){
                        body.find('#statusl').html("当前订单状态：拒绝退货");
                        body.find('#orderStatus').val("拒绝退货");
                    }else if(obj.data.orderStatus == 12){
                        body.find('#statusl').html("当前订单状态：待评论");
                        body.find('#orderStatus').val("待评论");
                    }else if(obj.data.orderStatus == 13){
                        body.find('#statusl').html("当前订单状态：已完成");
                        body.find('#orderStatus').val("已完成");
                    }
                    if(obj.data.orderSource == 2){
                        body.find('#orderSource').val("小程序");
                    }else if(obj.data.orderSource == 2){
                        body.find('#orderSource').val("APP");
                    }
                    if (obj.data.orderPayment == 1){
                        body.find('#orderPayment').val("微信支付");
                    }else if(obj.data.orderPayment == 2){
                        body.find('#orderPayment').val("余额支付");
                    }else if(obj.data.orderPayment == 3){
                        body.find('#orderPayment').val("支付宝支付");
                    }else if(obj.data.orderPayment == 4){
                        body.find('#orderPayment').val("未支付");
                    }
                }
            })
        }
    })
})
//查询所有订单
function squery(d) {
    console.log(d)
    orderStatusa = d;
    table.reload('btable', {
        where:{
            "orderStatus":d
        }
    });
}
